package final_project_combined;

/**
 * @author Jordan Barroga
 * @author Reed Pasatiempo
 */

import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;

import javax.swing.*;

public class SettingsMenu implements ActionListener
{
	
	static Settings temp;

	JFrame frame;
	
	static final int jSliderMin = 1;
	static final int jSliderMax = 4;
	static final int jSliderInit = 1;
	
	JRadioButton cyan = new JRadioButton("Cyan");
	JRadioButton blue = new JRadioButton("Blue");
	JRadioButton darkGray = new JRadioButton("Dark Gray");
	JRadioButton gray = new JRadioButton("Gray");
	JRadioButton green = new JRadioButton("Green");
	JRadioButton lightGray = new JRadioButton("Light Gray");
	JRadioButton magenta = new JRadioButton("Magenta");
	JRadioButton orange = new JRadioButton("Orange");
	JRadioButton pink = new JRadioButton("Pink");
	JRadioButton red = new JRadioButton("Red");
	JRadioButton white = new JRadioButton("White");
	JRadioButton yellow = new JRadioButton("Yellow");
	JRadioButton black = new JRadioButton("Black");

	JTextField speedInput = new JTextField(5);			// should this be a text field or a jslider?
	JTextField heightInput = new JTextField(5);
	JTextField widthInput = new JTextField(5);
	JSlider difficultyInput = new JSlider(JSlider.HORIZONTAL, jSliderMin, jSliderMax, jSliderInit);
	
	public SettingsMenu(Settings settings)
	{
		temp = settings;
		getDefaults();
		
		// JFrame 
		frame = new JFrame("Settings");
		Container test = frame.getContentPane();

		frame.setSize(500, 200);
		frame.setDefaultCloseOperation(frame.HIDE_ON_CLOSE);
		frame.setVisible(true);
		frame.setResizable(false);
		
		// Contents panel
		JPanel main = new JPanel();
		// Panel with exit buttons
		JPanel exit = new JPanel();
		
		// Tabbed Pane
		JTabbedPane menu = new JTabbedPane();
		main.add(menu);
		
		// Tabbed Panels
		JPanel snake = new JPanel();
		snake.setSize(300,400);
		
		JPanel board = new JPanel();
		board.setSize(300, 400);
		JPanel bg = new JPanel();
		bg.setSize(300, 400);
		
		// Tabbed Menu
		menu.addTab("Snake", snake);
		menu.addTab("Board", board);
		JPanel bgPanel = new JPanel(new GridLayout(3, 0));
		menu.add(bgPanel);
		bgPanel.setSize(300, 400);
		menu.addTab("Background", bgPanel);
		
		//==========================
		// TABBED SNAKE
		//==========================
		JLabel speedLabel = new JLabel("Speed: ");
		snake.add(speedLabel);
		snake.add(speedInput);
		// check if speed is within range
		JLabel difficultyLabel = new JLabel("Difficulty: ");
		snake.add(difficultyLabel);
		snake.add(difficultyInput);
		difficultyInput.setPaintLabels(true);
		difficultyInput.setPaintTicks(true);
		difficultyInput.setMinorTickSpacing(1);
		difficultyInput.setMajorTickSpacing(1);
		
		//==========================
		// TABBED BOARD
		//==========================
		JLabel heightLabel = new JLabel("Height: ");
		board.add(heightLabel);
		board.add(heightInput);
		// check if height is within range
		JLabel widthLabel = new JLabel("Width: ");
		board.add(widthLabel);
		board.add(widthInput);
		// check if width is within range
		
		//==========================
		// TABBED BACKGROUND
		//==========================
		// GOTTA "setMnemonic" and "setActionCommand"
		cyan.addActionListener(new ActionListener()
		{
			public void actionPerformed(ActionEvent e)
			{ 
				temp.setBackground(1); 
			}
		});
		blue.addActionListener(new ActionListener()
		{
			public void actionPerformed(ActionEvent e)
			{
				temp.setBackground(2);
			}
		});
		darkGray.addActionListener(new ActionListener()
		{
			public void actionPerformed(ActionEvent e)
			{
				temp.setBackground(3);
			}
		});
		gray.addActionListener(new ActionListener()
		{
			public void actionPerformed(ActionEvent e)
			{
				temp.setBackground(4);
			}
		});
		green.addActionListener(new ActionListener()
		{
			public void actionPerformed(ActionEvent e)
			{
				temp.setBackground(4);
			}
		});
		lightGray.addActionListener(new ActionListener()
		{
			public void actionPerformed(ActionEvent e)
			{
				temp.setBackground(5);
			}
		});
		magenta.addActionListener(new ActionListener()
		{
			public void actionPerformed(ActionEvent e)
			{
				temp.setBackground(6);
			}
		});
		orange.addActionListener(new ActionListener()
		{
			public void actionPerformed(ActionEvent e)
			{
				temp.setBackground(7);
			}
		});
		pink.addActionListener(new ActionListener()
		{
			public void actionPerformed(ActionEvent e)
			{
				temp.setBackground(8);
			}
		});
		red.addActionListener(new ActionListener()
		{
			public void actionPerformed(ActionEvent e)
			{
				temp.setBackground(9);
			}
		});
		white.addActionListener(new ActionListener()
		{
			public void actionPerformed(ActionEvent e)
			{
				temp.setBackground(10);
			}
		});
		yellow.addActionListener(new ActionListener()
		{
			public void actionPerformed(ActionEvent e)
			{
				temp.setBackground(11);
			}
		});
		black.addActionListener(new ActionListener()
		{
			public void actionPerformed(ActionEvent e)
			{
				temp.setBackground(12);
			}
		});
		black.setSelected(true);
		
		// adding to groups keeps multiples from being checked
		ButtonGroup bgGroup = new ButtonGroup();
		bgGroup.add(cyan);
		bgGroup.add(blue);
		bgGroup.add(darkGray);
		bgGroup.add(gray);
		bgGroup.add(green);
		bgGroup.add(lightGray);
		bgGroup.add(magenta);
		bgGroup.add(orange);
		bgGroup.add(pink);
		bgGroup.add(red);
		bgGroup.add(white);
		bgGroup.add(yellow);
		bgGroup.add(black);
		
		// adding them to the panel
		bgPanel.add(cyan);
		bgPanel.add(blue);
		bgPanel.add(darkGray);
		bgPanel.add(gray);
		bgPanel.add(green);
		bgPanel.add(lightGray);
		bgPanel.add(magenta);
		bgPanel.add(orange);
		bgPanel.add(pink);
		bgPanel.add(red);
		bgPanel.add(white);
		bgPanel.add(yellow);
		bgPanel.add(black);
		
		//==========================
		// EXIT PANEL
		//==========================

		// call to saveChanges
		JButton save = new JButton("Save Changes");
		save.addActionListener(new ActionListener()
		{
			public void actionPerformed(ActionEvent e)
			{
				//
			}
		});
		// make so button sets isModified to false
		JButton undo = new JButton("Undo Changes");
		undo.addActionListener(new ActionListener()
		{
			public void actionPerformed(ActionEvent e)
			{
				getDefaults();
			}
		});
		JButton def = new JButton("Default");
		def.addActionListener(new ActionListener()
		{
			public void actionPerformed(ActionEvent e)
			{
				reset();
			}
		});
		// closes this window and goes back to main
		JButton leave = new JButton("Exit");
		leave.addActionListener(new ActionListener()
		{
			public void actionPerformed(ActionEvent e)
			{
				frame.setVisible(false);
			}
		});
		
		exit.add(save);
		exit.add(undo);
		exit.add(def);
		exit.add(leave);
		
		Container pane = frame.getContentPane();
		
		pane.setLayout(new BorderLayout());
		frame.add(main);
		frame.add(exit);
		frame.getContentPane().add(main, BorderLayout.NORTH);
		frame.getContentPane().add(exit, BorderLayout.CENTER);
		
	}
	
	// This is just so i do not get an error not implementing a method
	public void actionPerformed(ActionEvent arg0) {
		
	}
	
	private void reset()
	{
		widthInput.setText("300");
		heightInput.setText("300");
		speedInput.setText("");
		saveChanges();
	}
	
	private void saveChanges()
	{
		// check the fields that need to be changed and save them
	}
	
	private boolean getDefaults()
	{
		boolean error = true;
		
		try
		{
			File inFile = new File("Default.txt");
			FileReader fReader = new FileReader(inFile);
			BufferedReader buff = new BufferedReader(fReader);
			
			speedInput.setText(buff.readLine());
			heightInput.setText(buff.readLine());
			widthInput.setText(buff.readLine());
			// need to check for difficulty
			
		}
		catch(NullPointerException e)
		{
			System.out.println("getDefaults: NullPointerException has occurred.");
		}
		catch(FileNotFoundException e)
		{
			System.out.println("getDefaults: FileNotFoundException has occurred.");
		}
		catch(IOException e)
		{
			System.out.println("getDefaults: IOException has occurred.");
		}
		
		return error;
	}

	private void writeChanges()
	{
		try
		{
			File inFile = new File("Default.txt");
			FileWriter fwriter = new FileWriter(inFile);
			BufferedWriter buff = new BufferedWriter(fwriter);
			
			buff.write(speedInput.getText());
			buff.write(heightInput.getText());
			buff.write(widthInput.getText());
			// need to set difficulty
			
			buff.close();
			
		}
		catch(IOException e)
		{
			System.out.println("writeChanges: IOException has occurred.");
		}
	}
	
}
